package cn.edu.gdut.csq.dao;

import java.util.LinkedList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.edu.gdut.csq.entity.Companyinfo;
import cn.edu.gdut.csq.entity.vo.StatisticData;

@Component
public class StatisticDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public Session openSession() {
		return sessionFactory.openSession();
	}

	public Session getSession() {

		// 事务必须是开启的(Required)，否则获取不到

		return sessionFactory.getCurrentSession();

	}

	public List<StatisticData> computeDistrictJobNum(String time) {

		String sql = "SELECT j.district , COUNT(j.jobId) FROM  Job j WHERE j.queryTime LIKE '%"
				+ time + "%' GROUP BY district order by COUNT(j.jobId) desc";

		// System.out.println(sql);
		Query query = getSession().createQuery(sql);
		List datas = query.list();
		List statisticDatas = new LinkedList<StatisticData>();
		for (Object object : datas) {
			Object[] data = (Object[]) object;
			StatisticData statisticData = new StatisticData();
			statisticData.setDistrict((String) data[0]);
			statisticData.setJobNum((long) data[1]);
			statisticDatas.add(statisticData);
		}

		// System.out.println(datas);
		return statisticDatas;

	}

}
